import pymysql
from flask import Flask,request,jsonify
import json
import random
import pymongo
from bson.objectid import ObjectId
import requests

app=Flask(__name__)
@app.route('/update_person_m',methods=['POST'])
def update_person_m():
    client=pymongo.MongoClient(host='127.0.0.1')['class_work']
    db_person=client['user_list']
    json=request.get_json()
    xy=json['xy']
    bj=json['bj']
    sign=json['sign']
    now_place=json['now_place']
    pre_place=json['pre_place']
    fdy=json['fdy']
    email=json['email']
    remark=json['remark']
    birthday=json['birthday']
    xx=json['xx']
    phone=json['phone']
    introduce=json['introduce']
    username=json['username']
    try:
        db_person.update_one({'username':username},{'$set':{'xy':xy,'bj':bj,'sign':sign,'now_place':now_place,'pre_place':pre_place,'fdy':fdy,'email':email,'remark':remark,'birthday':birthday,'xx':xx,'phone':phone,'introuduce':introduce}})
        return '1'
    except:
        return '0'
    
@app.route('/delete_friend',methods=['POST'])
def delete_friend():
    client=pymongo.MongoClient(host='127.0.0.1')['class_work']
    db_realation=client['user_realation']
    db_list=client['user_list']
    json=request.get_json()
    uid=json['uid']
    try:
        db_realation.delete_one({'username':uid})
        db_list.update_one({'username':uid},{'$set':{'fid':''}})
        return '1'
    except:
        return '0'
@app.route('/get_common',methods=['POST'])
def get_common():
    client=pymongo.MongoClient(host='127.0.0.1')['class_work']
    db_common=client['common']
    data=db_common.find({},{'_id':0})[0]
    return jsonify(data)
@app.route('/login',methods=['POST'])
def login():
    json=request.get_json()
    username=json['username']
    password=json['password']
    client=pymongo.MongoClient(host='127.0.0.1')['class_work']
    db_user=client['user_account']
    data=db_user.find_one({'username':username,'password':password})
    post_data={
        'xh':'202005060234',
        'xh_mm':'hgzs.321',
        'year':2020,
        'term':3
    }
    grade_info=requests.post(url='http://103.214.171.186/grade')
    result={}
    if data!=None:
        result['uid']=str(data['_id'])
        result['username']=data['username']
        result['sex']=data['sex']
        result['place']=data['place']
        result['ctime']=data['ctime']
        result['name']=data['name']
        result['class_list']=data['class_list']
        result['grade']=data['grade']
        result['xf_detials']=data['xf_detials']
        result['fdy']=data['fdy']
            
        return jsonify(result)
    else:
        return '0'
@app.route('/get_realation',methods=['POST'])
def get_friends():
    json=request.get_json()
    uid=json['uid']
    client=pymongo.MongoClient(host='127.0.0.1')['class_work']
    db_friend=client['user_realation']
    data=db_friend.find({'uid':uid},{'_id':0})
    result=[]
    temp={}
    for i in data:
        temp['fid']=i['fid']
        temp['place']=i['place']
        result.append(temp.copy())
    return jsonify(result)
@app.route('/get_person_message',methods=['POST'])
def get_person_message():
    json=request.get_json()
    username=json['username']
    clien=pymongo.MongoClient(host='127.0.0.1')['class_work']
    db_person_message=clien['user_list']
    data=db_person_message.find_one({'username':username},{'_id':0})
    return jsonify(data)
@app.route('/update_person_message',methods=['POST'])
def update_person_message():
    json=request.get_json()
    username=json['username']
    bz=json['bz']
    fid=json['fid']
    clien=pymongo.MongoClient(host='127.0.0.1')['class_work']
    db_person_message=clien['user_list']
    try:
        db_person_message.update({'username':fid,'fid':username},{'$set':{'bz':bz}})
        return '1'
    except:
        return '0'
@app.route('/update_person_page',methods=['POST'])
def update_person_page():
    json=request.get_json()
    user=json['user']
    name=json['name']
    sex=json['sex']
    birthday=json['birthday']
    xx=json['xx']
    sign=json['sign']
    id=json['id']
    xy=json['xy']
    bj=json['bj']
    now_place=json['now_place']
    pre_place=json['pre_place']
    phone=json['phone']
    email=json['email']
    fdy=json['fdy']
    introuduce=json['introuduce']
    client=pymongo.MongoClient(host='127.0.0.1')['class_work']
    db_person_page=client['user_list']
    try:
        db_person_page.update_one({'username':user},{'$set':{'xy':xy,'bj':bj,'sign':sign,'now_place':now_place,'pre_place':pre_place,'fdy':fdy,'email':email,'birthday':birthday,'xx':xx,'phone':phone,'introuduce':introuduce}})
        return '1'
    except:
        return '0'
if __name__=='__main__':
    app.run(host='127.0.0.1',port=5000)
